//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartPyramidView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartPyramidView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartPyramidView

IMPLEMENT_DYNCREATE(CChartPyramidView, CBCGPChartExampleView)

CChartPyramidView::CChartPyramidView()
	: CBCGPChartExampleView(CChartPyramidView::IDD)
{
	//{{AFX_DATA_INIT(CChartPyramidView)
	m_bShowDataLabels = TRUE;
	m_nDataLabelPosition = 0;
	m_nVerticalGap = 5;
	m_bDataLabelsInColimns = FALSE;
	m_bCircleBase = FALSE;
	m_nTransparency = 10;
	//}}AFX_DATA_INIT
}

CChartPyramidView::~CChartPyramidView()
{
}

void CChartPyramidView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartPyramidView)
	DDX_Control(pDX, IDC_ROTATE, m_wndRotate);
	DDX_Control(pDX, IDC_TRANSPARENCY, m_wndTransparency);
	DDX_Control(pDX, IDC_DATA_LABEL_IN_COLUMNS, m_wndDataLabelsInColumns);
	DDX_Control(pDX, IDC_CIRCLE_BASE, m_wndCircleBase);
	DDX_Control(pDX, IDC_VERTICAL_GAP, m_wndVerticalGap);
	DDX_Control(pDX, IDC_DATA_LABEL_POSITION, m_wndDataLabelPosition);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Check(pDX, IDC_DATA_LABELS, m_bShowDataLabels);
	DDX_CBIndex(pDX, IDC_DATA_LABEL_POSITION, m_nDataLabelPosition);
	DDX_Slider(pDX, IDC_VERTICAL_GAP, m_nVerticalGap);
	DDX_Check(pDX, IDC_DATA_LABEL_IN_COLUMNS, m_bDataLabelsInColimns);
	DDX_Check(pDX, IDC_CIRCLE_BASE, m_bCircleBase);
	DDX_Slider(pDX, IDC_TRANSPARENCY, m_nTransparency);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartPyramidView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartPyramidView)
	ON_BN_CLICKED(IDC_DATA_LABELS, OnUpdateChart)
	ON_WM_HSCROLL()
	ON_CBN_SELENDOK(IDC_DATA_LABEL_POSITION, OnUpdateChart)
	ON_BN_CLICKED(IDC_DATA_LABEL_IN_COLUMNS, OnUpdateChart)
	ON_BN_CLICKED(IDC_CIRCLE_BASE, OnUpdateChart)
	ON_BN_CLICKED(IDC_ROTATE, OnRotate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartPyramidView diagnostics

#ifdef _DEBUG
void CChartPyramidView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartPyramidView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartPyramidView message handlers

void CChartPyramidView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	if (m_Feature == CDemoFeature::BCGP_Pyramid)
	{
		m_wndCircleBase.EnableWindow(FALSE);
	}

	CBCGPRotationObject* pRO = m_wndRotate.GetRotationObject();
	ASSERT_VALID(pRO);

	pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_CLOCKWISE, FALSE);
	pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_NARROW_FIELD_OF_VIEW, FALSE);
	pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_WIDEN_FIELD_OF_VIEW, FALSE);
	pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_COUNTER_CLOCKWISE, FALSE);

	pRO->SetAutorepeatMode(100);
	pRO->SetColorTheme(CBCGPRotationObject::BCGP_COLOR_THEME_VISUAL_MANAGER);
	pRO->EnableFlatIcons();

	m_wndRotate.EnableWindow(m_Feature == CDemoFeature::BCGP_Pyramid3D);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartType(m_Feature == CDemoFeature::BCGP_Pyramid3D ? BCGPChartPyramid3D : BCGPChartPyramid);

	pChart->SetChartTitle(_T("Site visitors from US:"));

	pChart->AddChartData(_T("California"), 6700);
	pChart->AddChartData(_T("Virginia"), 4500);
	pChart->AddChartData(_T("New York"), 2200);
	pChart->AddChartData(_T("Pennsylvania"), 1500);
	pChart->AddChartData(_T("Georgia"), 1400);
	pChart->AddChartData(_T("Colorado"), 1350);
	pChart->AddChartData(_T("Other States"), 1700);

	m_wndVerticalGap.SetRange(0, 20, TRUE);
	m_wndVerticalGap.SetPos(m_nVerticalGap);

	m_wndTransparency.SetRange(0, 100, TRUE);

	pChart->SetSeriesShadow(m_Feature != CDemoFeature::BCGP_Pyramid3D);

	m_bIsReady = TRUE;

	OnUpdateChart();
}

void CChartPyramidView::OnUpdateChart() 
{
	UpdateData();

	m_wndDataLabelPosition.EnableWindow(m_bShowDataLabels);
	m_wndDataLabelsInColumns.EnableWindow(m_bShowDataLabels);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->ShowDataLabels(m_bShowDataLabels);

	if (m_bShowDataLabels)
	{
		BCGPChartDataLabelOptions dataLabelOptions = pChart->GetDataLabelOptions();

		switch (m_nDataLabelPosition)
		{
		case 0:
			dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_DEFAULT_POS;
			break;

		case 1:
			dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_OUTSIDE_END;
			dataLabelOptions.m_dblAngle = -90;
			break;

		case 2:
			dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_OUTSIDE_END;
			dataLabelOptions.m_dblAngle = 90;
			break;

		case 3:
			dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_CENTER;
			m_wndDataLabelsInColumns.EnableWindow(FALSE);
			break;
		}

		pChart->SetDataLabelsOptions(dataLabelOptions);
	}

	for (int i = 0; i < pChart->GetSeriesCount(); i++)
	{
		CBCGPChartPyramidSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPyramidSeries, pChart->GetSeries(i));
		ASSERT_VALID(pSeries);

		pSeries->SetCircularBase(m_bCircleBase);
		pSeries->m_bDataLabelsInColumns = m_bDataLabelsInColimns;
		pSeries->SetGap(m_nVerticalGap);

		if (m_bCircleBase)
		{
			pSeries->SetDepthPercent(10);
		}
	}

	m_wndRotate.ShowWindow(m_bCircleBase ? SW_HIDE : SW_SHOWNOACTIVATE);

	pChart->SetThemeOpacity(100 - m_nTransparency);
	pChart->Redraw();
}

void CChartPyramidView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CBCGPChartExampleView::OnHScroll(nSBCode, nPos, pScrollBar);

	if (pScrollBar != NULL)
	{
		OnUpdateChart();
	}
}

void CChartPyramidView::OnRotate() 
{
	UpdateData();

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	double xDelta = 10;
	int yDelta = 5;

	for (int i = 0; i < pChart->GetSeriesCount(); i++)
	{
		CBCGPChartPyramidSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPyramidSeries, pChart->GetSeries(i));
		ASSERT_VALID(pSeries);

		double nRotationAngle = pSeries->GetRotation();
		int nDepth = pSeries->GetDepthPercent();

		switch (m_wndRotate.GetRotationObject()->GetClicked())
		{
		case CBCGPRotationObject::BCGP_ROTATION_UP:
			nDepth -= yDelta;
			break;

		case CBCGPRotationObject::BCGP_ROTATION_DOWN:
			nDepth += yDelta;
			break;

		case CBCGPRotationObject::BCGP_ROTATION_LEFT:
			nRotationAngle -= xDelta;
			break;

		case CBCGPRotationObject::BCGP_ROTATION_RIGHT:
			nRotationAngle += xDelta;
			break;

		case CBCGPRotationObject::BCGP_ROTATION_RESET:
			nRotationAngle = 0;
			nDepth = 10;
			break;
		}

		pSeries->SetRotation(nRotationAngle);
		pSeries->SetDepthPercent(nDepth);
	}

	pChart->SetDirty();
	pChart->Redraw();
}
